package com.snowleopard.findpeople.web.position;

import java.util.List;

import org.apache.struts2.convention.annotation.Namespace;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springside.modules.utils.web.struts2.Struts2Utils;

import com.opensymphony.xwork2.ActionSupport;
import com.snowleopard.common.param.ParamObject;
import com.snowleopard.findpeople.service.position.AreaManager;
import com.snowleopard.findpeople.service.position.TradeManager;

@Namespace("/position")
public class AreaTradeTreeAction extends ActionSupport {
    private static final Logger log = LoggerFactory.getLogger(AreaTradeTreeAction.class);
    private static final long serialVersionUID = 1472939343644324456L;

    @Autowired
    private AreaManager areaManager;
    @Autowired
    private TradeManager tradeManager;

    private String clickedAreaId;
    private String clickedTradeId;

    private String areaTreeData;
    private String tradeTreeData;

    @Override
    public String execute() throws Exception {
        // TODO Get the location from external web service
        String initialArea = "china";
        areaTreeData = areaManager.getTree(initialArea);

        // TODO Get the trade from IP cache
        String initialTrade = "trade";
        tradeTreeData = tradeManager.getTree(initialTrade);

        return SUCCESS;
    }

    public void ajaxGetAreaTree() {
        areaTreeData = areaManager.getTree(clickedAreaId);
        Struts2Utils.renderXml(areaTreeData);
    }

    public void ajaxGetTradeTree() {
        tradeTreeData = tradeManager.getTree(clickedTradeId);
        Struts2Utils.renderXml(tradeTreeData);
    }

    public void ajaxGetAdjustTrades() {
        List<ParamObject> trades = tradeManager.findChildren(clickedTradeId);

        Struts2Utils.renderJson(trades);
    }

    public void ajaxGetAdjustAreas() {
        List<ParamObject> trades = areaManager.findChildren(clickedAreaId);

        Struts2Utils.renderJson(trades);
    }

    public String getAreaTreeData() {
        return areaTreeData;
    }

    public void setAreaTreeData(String areaTreeData) {
        this.areaTreeData = areaTreeData;
    }

    public String getTradeTreeData() {
        return tradeTreeData;
    }

    public void setTradeTreeData(String tradeTreeData) {
        this.tradeTreeData = tradeTreeData;
    }

    public String getClickedAreaId() {
        return clickedAreaId;
    }

    public void setClickedAreaId(String clickedAreaId) {
        this.clickedAreaId = clickedAreaId;
    }

    public String getClickedTradeId() {
        return clickedTradeId;
    }

    public void setClickedTradeId(String clickedTradeId) {
        this.clickedTradeId = clickedTradeId;
    }

}
